package utils.system;

import com.sun.codemodel.internal.JOp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import utils.file.FileSystemModelUtils;
import utils.properties.YourJavaPropertiesUtils;

/**
 *
 * @author Juan Carlos García
 */
public class SystemUtils {
    
    
    /**
     * This method creates the necesary code to start a new class
     * 
     * @param className ClassName that we're going to insert in our newClass
     * @return The necessary code to create a Class
     */
    public static String createNewClass(String className){
        
        String newClassCode = "/**\n*\n* @author yourJava \n*/\n\n public class "+className+"{\n\n\n }";
        
        return newClassCode;
        
    }
    
    /**
     * This method refresh the JTree changing the TreeModel
     * 
     * @param directory 
     */
    public static void refreshTree(JTree fileTree,String directory){
       
        FileSystemModelUtils fileSystemModel = new FileSystemModelUtils(new File(directory));
        fileTree.setModel(fileSystemModel);
        
    }
    

    /**
     * This method creates a new project in the current workspace
     * 
     * @param directory Current Workspace
     * @param fileTree FileTree to refres
     */
    public static void createProject(String directory,JTree fileTree,String projectName){
        
        String projectPath = directory+"/"+projectName;
        
        boolean projectCreation = new File(projectPath).mkdirs();
        projectCreation = new File(projectPath+"/src").mkdirs();
        //TODO: Create new class and save in /src
        projectCreation = new File(projectPath+"/lib").mkdirs();
        
        if(projectCreation){
            
            SystemUtils.refreshTree(fileTree, directory);
            
            JOptionPane.showMessageDialog(null,"Project created successfully");
            
            YourJavaPropertiesUtils.setProperty("currentProject",projectPath,"properties");
            
        }else{
            
            JOptionPane.showMessageDialog(null,"Error creating project");
        }
        
    }
    
    /**
     * This method creates a new dictionary with the parsed data sent by the 
     * system
     * 
     * @param title Dictionary Title
     * @param version Dictionary Version
     * @param author Dictionary Author
     * @param data Dictionary Data
     * @param rows Dictionary Rows
     * @param columns Dictionary Columns
     */
    public static void createNewDictionary(String title,String version,String author,String[][] data,int rows,int columns){
        String header = "#:##############################################################################\n"
                + "#:##############################################################################\n"
                + "#:######################    yourJava Dictionaries  #############################\n"
                + "#:##############################################################################\n"
                + "#:##############################################################################\n\n"
                + "#: Welcome to dictionary template. To add a comment write # and continue\n"
                + "#: with the comment.\n\n"
                + "#: In the next lines you can find some properties about this dictionary.\n\n"
                + "#: You can add reserved words with your own value, like Key-Value\n\n"
                + "#: You can use the dictionary editor included in yourJava application.\n\n"
                + "#:########### Dictionary Properties ############\n\n"
                + "#Title: "+title+"\n"
                + "#Version:"+version+"\n"
                + "#Author: "+author+"\n\n"
                + "#:########### Values ############\n\n";
        String line = "";
        FileWriter dictionary = null;
        PrintWriter pw = null;
        try{
            String route = System.getProperty("user.dir");
            dictionary = new FileWriter(route+"/src/resources/dictionaries/"+title+".yjd");
            pw = new PrintWriter(dictionary);
            pw.println(header);
            for (int i = 0; i < rows; i++){
                for(int x=0;x<columns;x++){
                    if(x==0){
                       line = data[i][x];
                    }else{
                        if(data[i][x]!=""){
                            line+=":"+data[i][x];
                            pw.println(line);
                        }
                    }    
                }
            }
                
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e.getMessage());
        }finally {
           try {
           if (null != dictionary)
              dictionary.close();
              JOptionPane.showMessageDialog(null,"Your dictionary "+title+" was created");
           } catch (Exception e2) {
              JOptionPane.showMessageDialog(null, e2.getMessage());
           }
        }
        
    }
    
    public static ArrayList getDictionaryNames(){
        String route = System.getProperty("user.dir")+"/src/resources/dictionaries";
        File f = new File(route);
        File[] files = f.listFiles();
        ArrayList names = new ArrayList();
        for (int x=0;x<files.length;x++){
            try{
                String name = files[x].getName().replaceAll(".yjd","");
                name=name.replaceAll(".svn","");
                if(!name.equals("")){
                    FileInputStream fstream = new FileInputStream(route+"/"+name+".yjd");
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String strLine;
                    String author = "";
                    String version = "";
                    while ((strLine = br.readLine()) != null)   {
                         if(strLine.contains("#Version:")){
                             version = strLine.substring(9).trim();
                         }
                         if(strLine.contains("#Author:")){
                             author = strLine.substring(9).trim();
                         }                         
                    }
                    names.add(name + " - v" + version + " - " + author);
                }
            }catch(Exception e2){
                JOptionPane.showMessageDialog(null,"Error loading dictionaries");
            }
        }
        
        return names;
    }
    
}
